home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
rjs.lha
/
RJS
/
CmdLine
/
src
/
CmdLine.C.bak
< prev
next >
Wrap
Text File
|
1991-06-14
|
4KB
|
164 lines
#include "CmdLine.h"
#include <stdlib.h>
//#include <string.h>
#include <osfcn.h>
#include <iostream.h>
CmdLine::CmdLine(RJS_CmdOpt **opt, const char *p, CmdLineFlags f)
{
prefix=p;
flags=f;
options=opt;
if (abbrev()) setup_abbrevs();
else for (int i=0; options[i] != NULL; i++)
options[i]->abbrev_len = options[i]->keyword().length();
if (dcl()) sep='/'; else sep='-';
}
void CmdLine::setup_abbrevs()
{
int i,j,l,min_len;
for (i=0; options[i] != NULL; i++) {
options[i]->abbrev_len=1;
for (j=0; options[j] != NULL; j++) if (i==j) continue; else {
min_len= (options[i]->keyword().length() <= options[j]->keyword().length()) ?
options[i]->keyword().length() : options[j]->keyword().length();
for (l=0; l<min_len; l++)
if (options[i]->keyword()[l] != options[j]->keyword()[l]) break;
l++;
if (l > options[i]->abbrev_len) {
if (l < options[i]->keyword().length())
options[i]->abbrev_len=l;
else
options[i]->abbrev_len=options[i]->keyword().length();
}
}
}
}
CmdLine::ParseStatus CmdLine::parse(char **argv, String &xtra)
{
CmdLineWord clw(argv);
return parse(clw,xtra);
}
CmdLine::ParseStatus CmdLine::parse(String &cmdline, String &xtra)
{
CmdLineWord clw(cmdline,dcl());
return parse(clw,xtra);
}
CmdLine::ParseStatus CmdLine::parse(CmdLineWord &clw, String &xtra)
{
xtra="";
int i;
int no_more_options=0;
for (i=0; options[i] != NULL; i++) options[i]-> reset();
while (clw(opt)) {
if (opt.contains('=')) {
val=opt.after('=');
opt=opt.before('=');
} else val="";
if (opt[0]!=sep || opt=='-' || no_more_options) {
// parameter checking here!
xtra += (opt + ' ');
continue;
} else if ( opt.length()>1 && opt[0]=='-' && opt[1]=='-') {
no_more_options=1;
continue;
}
String copt = opt(1); // remove separtor ('-'|'/')
for (i=0; options[i] != NULL; i++) {
if (copt.length() > options[i]->keyword().length() ||
copt.length() < options[i]->abbrev_len) continue;
if (no_case()) {
if (options[i]->keyword().substr(0,copt.length())==copt.icase())
break;
}
else if (options[i]->keyword().substr(0,copt.length())==copt) break;
}
if (options[i]==NULL) {
if (messages())
cerr << prefix << ": error unknown option: "<< opt << endl;
if (abort()) exit(1); else return UnknownOption;
}
if (options[i]->is_present() && only_once()) {
if (messages())
cerr << prefix << ": option specified more then once:" <<sep<< opt << endl;
if (abort()) exit(1); else return MoreThenOnce;
}
if (options[i]->expects_value()) { // option has a required valu
if (val=="") {
if (options[i]->value_optional()) {
String temp;
if(clw.peek(temp) && (temp[0]!=sep || temp=="--"))
clw(val);
else {
options[i]->set();
continue; // get next option
}
} else clw(val);
}
if (!val) {
if (messages())
cerr << prefix << ": missing " << options[i]->value_type()
<< " arguement for " <<sep<< options[i]->keyword() << endl;
if (abort()) exit(1); else return NoValue;
} else {
if (!options[i]->set(val)) {
if (messages())
cerr << prefix << ": invalid arguement for "<<sep <<
options[i]->keyword() << ", expecting "<<
options[i]->value_type() << endl;
if (abort()) exit(1); else return BadValue;
}
}
} else options[i]->set();
} // end of while
for (i=0; options[i] != NULL; i++) {
if (options[i]->is_required() && !options[i]->is_present()) {
if (messages())
cerr << prefix << ": "<<sep << options[i]->keyword() <<
" is required" << endl;
if (abort()) exit(1); else return MissingRequired;
}
if (options[i]->has_default() && !options[i]->is_present()) {
if (!options[i]->set_default()) {
if (messages())
cerr << prefix << ": invalid arguement for "<<sep <<
options[i]->keyword() << ", expecting "<<
options[i]->value_type() << endl;
if (abort()) exit(1); else return BadValue;
}
}
}
return Ok;
}
void CmdLine::dump()
{
for (int i=0; options[i] != NULL; i++) options[i]->dump();
}